﻿using System;
using System.Collections.Generic;
using Flagwind.Options;
using Flagwind.Options.Configuration;

namespace Flagwind.Options.Plugins
{
	internal static class PluginSettingsProviderFactory
	{
		#region 私有变量

		private static readonly Flagwind.Collections.ObjectCache<PluginSettingsProvider> _cache;

		#endregion

		#region 静态构造

		static PluginSettingsProviderFactory()
		{
			_cache = new Collections.ObjectCache<PluginSettingsProvider>(0);
		}

		#endregion

		#region 公共方法

		public static PluginSettingsProvider GetProvider(Flagwind.Plugins.Plugin plugin)
		{
			if(plugin == null)
			{
				throw new ArgumentNullException("plugin");
			}

			var configuration = OptionUtility.GetConfiguration(plugin);

			if(configuration == null)
			{
				return null;
			}

			return _cache.Get(plugin.FilePath, key =>
			{
				return new PluginSettingsProvider(plugin, configuration);
			});
		}

		#endregion
	}
}